package algorithm.middle;

import java.util.ArrayList;
import java.util.List;

public class GrayCode89 {
    /**
     * https://leetcode-cn.com/problems/gray-code/solution/gray-code-jing-xiang-fan-she-fa-by-jyd/
     * 镜像反射法
     * @param n
     * @return
     */
    public static List<Integer> grayCode(int n) {
        ArrayList<Integer> res = new ArrayList<>();
        res.add(0);
        int head = 1;
        for (int i = 0; i < n; i++) {
            for (int j = res.size() - 1; j > -1; j--) {
                res.add(head + res.get(j));
            }
            head = head << 1;
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(grayCode(2));
    }
}
